package com.example.tiantian.battery;

import com.example.tiantian.BaseActivity;
import com.example.tiantian.OnReceiveListener;
import com.example.tiantian.R;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class BatteryChargeActivity extends BaseActivity implements OnReceiveListener {

    static final int COLOR_SAFE = Color.GREEN;
    static final int COLOR_WARNING = Color.YELLOW;
    static final int COLOR_DANGER = Color.RED;

    ImageView imgBatteryLevel;
    TextView txtBatteryLevel;

    TextView txtQ;
    TextView txtR;
    TextView txtS;

    BatteryReceiver receiver;
    IntentFilter filter;

    Drawable active;
    Drawable deactive;

    @Override
    protected void init() {
        setContentView(R.layout.activity_battery_charge);

        imgBatteryLevel = (ImageView) findViewById(R.id.battery_info_image);
        txtBatteryLevel = (TextView) findViewById(R.id.battery_info_level);

        txtQ = (TextView) findViewById(R.id.battery_charge_q);
        txtR = (TextView) findViewById(R.id.battery_charge_r);
        txtS = (TextView) findViewById(R.id.battery_charge_s);

        receiver = new BatteryReceiver();
        filter = new IntentFilter();
    }

    @Override
    protected void loadData() {
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);

        active = getResources().getDrawable(R.drawable.battery_bulb_active);
        deactive = getResources().getDrawable(R.drawable.battery_bulb_deactive);
        active.setBounds(0, 0, active.getMinimumWidth(), active.getMinimumHeight());
        deactive.setBounds(0, 0, deactive.getMinimumWidth(), deactive.getMinimumHeight());

        txtQ.setCompoundDrawablePadding(5);
        txtR.setCompoundDrawablePadding(5);
        txtS.setCompoundDrawablePadding(5);
    }

    @Override
    protected void event() {
        receiver.SetOnReceiveListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        int batteryLevel = extras.getInt(BatteryManager.EXTRA_LEVEL, 0);
        int batteryStateCharge = extras.getInt(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN);
        if (batteryStateCharge == BatteryManager.BATTERY_STATUS_CHARGING) {

            if (batteryLevel == 100) {
                imgBatteryLevel.setImageResource(R.drawable.bt100);
            } else if (batteryLevel >= 90) {
                imgBatteryLevel.setImageResource(R.drawable.bt90);
            } else if (batteryLevel >= 80) {
                imgBatteryLevel.setImageResource(R.drawable.bt80);
            } else if (batteryLevel >= 70) {
                imgBatteryLevel.setImageResource(R.drawable.bt70);
            } else if (batteryLevel >= 50) {
                imgBatteryLevel.setImageResource(R.drawable.bt50);
            } else if (batteryLevel >= 30) {
                imgBatteryLevel.setImageResource(R.drawable.bt30);
            } else if (batteryLevel >= 20) {
                imgBatteryLevel.setImageResource(R.drawable.bt20);
            } else if (batteryLevel >= 10) {
                imgBatteryLevel.setImageResource(R.drawable.bt10);
            } else {
                imgBatteryLevel.setImageResource(R.drawable.bt0);
            }

            txtBatteryLevel.setText(batteryLevel + "%");
            txtBatteryLevel.setTextColor(
                    batteryLevel >= 70 ? COLOR_SAFE : (batteryLevel >= 20) ? COLOR_WARNING : COLOR_DANGER);

            if (batteryLevel < 80) {
                txtQ.setCompoundDrawables(null, active, null, null);
                txtR.setCompoundDrawables(null, deactive, null, null);
                txtS.setCompoundDrawables(null, deactive, null, null);
            } else if (batteryLevel < 98) {
                txtQ.setCompoundDrawables(null, deactive, null, null);
                txtR.setCompoundDrawables(null, active, null, null);
                txtS.setCompoundDrawables(null, deactive, null, null);
            } else {
                txtQ.setCompoundDrawables(null, deactive, null, null);
                txtR.setCompoundDrawables(null, deactive, null, null);
                txtS.setCompoundDrawables(null, active, null, null);
            }

        } else {
            txtQ.setCompoundDrawables(null, deactive, null, null);
            txtR.setCompoundDrawables(null, deactive, null, null);
            txtS.setCompoundDrawables(null, deactive, null, null);
        }
    }

}
